<?php

class CCoreBaseHttp
{
	#<editor-fold defaultstate="collapsed" desc="private">
	/**
	 * @var	bool
	 */
	private $_bIsMagicQuotesOn;
	#</editor-fold>

	/**
	 * @return	CCoreBaseHttp
	 */
	public function __construct()
	{
		$this->_bIsMagicQuotesOn = (bool) ini_get('magic_quotes_gpc');
	}

	/**
	 * @param	string	$sKey
	 * @param	mixed	$nmDefault = null
	 * @return	mixed
	 */
	public function GetQuery($sKey, $nmDefault = null)
	{
		CCore::CheckTypes(array('$sKey', '$nmDefault'));
		
		return (isset($_GET[$sKey]))
			? $this->_stripSlashesValue($_GET[$sKey]) : $nmDefault;
	}

	/**
	 * @param	string	$sKey
	 * @param	mixed	$nmDefault = null
	 * @return	mixed
	 */
	public function GetPost($sKey, $nmDefault = null)
	{
		CCore::CheckTypes(array('$sKey', '$nmDefault'));

		return (isset($_POST[$sKey]))
			? $this->_stripSlashesValue($_POST[$sKey]) : $nmDefault;
	}

	/**
	 * @param	string	$sKey
	 * @param	mixed	$nmDefault = null
	 * @return	mixed
	 */
	public function GetRequest($sKey, $nmDefault = null)
	{
		return (isset($_REQUEST[$sKey]))
			? $this->_stripSlashesValue($_REQUEST[$sKey]) : $nmDefault;
	}
	
	/**
	 * @param	string	$sKey
	 * @param	mixed	$nmDefault = null
	 * @return	mixed
	 */
	public function GetCookie($sKey, $nmDefault = null)
	{
		CCore::CheckTypes(array('$sKey', '$nmDefault'));

		return (isset($_COOKIE[$sKey]))
			? $this->_stripSlashesValue($_COOKIE[$sKey]) : $nmDefault;
	}

	/**
	 * @param	string	$sKey
	 * @param	mixed	$nmDefault = null
	 * @return	mixed
	 */
	public function GetServer($sKey, $nmDefault = null)
	{
		CCore::CheckTypes(array('$sKey', '$nmDefault'));

		return (isset($_SERVER[$sKey])) ? $_SERVER[$sKey] : $nmDefault;
	}

	/**
	 * @param	string	$sKey
	 * @param	mixed	$nmDefault = null
	 * @return	mixed
	 */
	public function GetEnv($sKey, $nmDefault = null)
	{
		CCore::CheckTypes(array('$sKey', '$nmDefault'));

		return (isset($_ENV[$sKey])) ? $_ENV[$sKey] : $nmDefault;
	}

	/**
	 * @return	string
	 */
	public function GetMethod()
	{
		return $this->GetServer('REQUEST_METHOD', '');
	}

	/**
	 * @return	bool
	 */
	public function IsPost()
	{
		return ('POST' === $this->GetMethod());
	}

	/**
	 * @return	bool
	 */
	public function IsGet()
	{
		return ('POST' === $this->GetMethod());
	}

	/**
	 * @return	string | false
	 */
	public function GetRawBody()
	{
		static $sRawBody = null;
		if (null === $sRawBody)
		{
			$sBody = @file_get_contents('php://input');
			$sRawBody = (strlen(trim($sBody)) > 0) ? $sBody : false;
		}
		return $sRawBody;
	}

	/**
	 * @param	string	$sHeader
	 * @return	string | false
	 */
	public function GetHeader($sHeader)
	{
		CCore::CheckTypes(array('$sHeader'));

		$sServerKey = 'HTTP_' . strtoupper(str_replace('-', '_', $sHeader));
		if (isset($_SERVER[$sServerKey]))
		{
			return $_SERVER[$sServerKey];
		}

		if (function_exists('apache_request_headers'))
		{
			$sHeaders = @apache_request_headers();
			if (isset($sHeaders[$sHeader]))
			{
				return $sHeaders[$sHeader];
			}
		}

		return false;
	}

	/**
	 * @return	string
	 */
	public function GetScheme()
	{
		return ($this->GetServer('HTTPS') == 'on') ? 'https' : 'http';
	}

	/**
	 * @return	bool
	 */
	public function IsSecure()
	{
		return ('https' === $this->GetScheme());
	}

	/**
	 * @return	string
	 */
	public function GetHost()
	{
		$sHost = $this->GetServer('HTTP_HOST');
		if (!empty($sHost))
		{
			return $sHost;
		}

		$sScheme = $this->GetScheme();
		$sName = $this->GetServer('SERVER_NAME');
		$iPort = (int) $this->GetServer('SERVER_PORT');

		return (('http' === $sScheme && 80 === $iPort) || ('https' === $sScheme && 443 === $iPort))
			? $sName : $sName.':'.$iPort;
	}

	/**
	 * @param	bool	$bCheckProxy = true
	 * @return	string
	 */
	public function GetClientIp($bCheckProxy = true)
	{
		CCore::CheckTypes(array('$bCheckProxy'));

		$sIp = '';
		if ($bCheckProxy && null !== $this->GetServer('HTTP_CLIENT_IP', null))
		{
			$sIp = $this->GetServer('HTTP_CLIENT_IP', '');
		}
		else if ($bCheckProxy && null !== $this->GetServer('HTTP_X_FORWARDED_FOR', null))
		{
			$sIp = $this->GetServer('HTTP_X_FORWARDED_FOR', '');
		}
		else
		{
			$sIp = $this->GetServer('REMOTE_ADDR', '');
		}

		return $sIp;
	}

	/**
	 * @param	mixed	$mValue
	 * @return	mixed
	 */
	private function _stripSlashesValue($mValue)
	{
		CCore::CheckTypes(array('$mValue'));

		if (!$this->_bIsMagicQuotesOn)
		{
			return $mValue;
		}
		
		$sType = gettype($mValue);
		if ($sType === 'string')
		{
			return stripslashes($mValue);
		}
		else if ($sType === 'array')
		{
			$aReturnValue = array();
			$mValueKeys = array_keys($mValue);
			foreach($mValueKeys as $sKey)
			{
				$aReturnValue[$sKey] = $this->_stripSlashesValue($mValue[$sKey]);
			}
			return $aReturnValue;
		}
		else
		{
			return $mValue;
		}
	}
}